package com.css.fxfzmhwz.common.utils.entity;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Data
public class Polygon implements Serializable {

    public List<Point> points;//第一个面
    public List<List<Point>> noPointsList;//镂空面数组

    public Polygon() {

    }
    public Polygon(String geometry) {
        List<Point> pointArray0 = null;//面点数组
        List<List<Point>> noPointsList = null;//镂空的面点数组列表
        List<Point> pointArray = null;
        Point point = null;

        if(geometry.indexOf("#")!=-1) {//单个面（包括镂空的面）
            noPointsList = new ArrayList<List<Point>>();
            String[] subGeometries = StringUtils.split(geometry, "#");
            for (int k = 0; k < subGeometries.length; k++) {
                String subGeometry = subGeometries[k];
                String[] lonLats = subGeometry.split(", ");
                pointArray = new ArrayList<Point>();
                for (int l = 0; l < lonLats.length; l++) {
                    String[] lonLat = StringUtils.split(lonLats[l], " ");
                    point = new Point(Double.parseDouble(lonLat[0]), Double.parseDouble(lonLat[1]));
                    pointArray.add(point);
                }
                if(k==0){
                    pointArray0 = pointArray;
                }else{
                    noPointsList.add(pointArray);
                }
            }
        }else{//单个面（不包括镂空的面）
            String[] lonLats = geometry.split(", ");
            pointArray0 = new ArrayList<Point>();
            for (int l = 0; l < lonLats.length; l++) {
                String[] lonLat = StringUtils.split(lonLats[l], " ");
                point = new Point(Double.parseDouble(lonLat[0]), Double.parseDouble(lonLat[1]));
                pointArray0.add(point);
            }
        }
        this.points = pointArray0;
        this.noPointsList = noPointsList;
    }
}
